package com.huike.web.controller.system;

import com.huike.clues.service.ISysMenuService;
import com.huike.common.core.domain.AjaxResult;
import com.huike.common.core.domain.TreeSelect;
import com.huike.common.core.domain.entity.SysMenu;
import com.huike.common.utils.SecurityUtils;
import com.huike.web.result.RoleDeptTreeAjaxResult;
import com.huike.web.result.RoleMenuTreeAjaxResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author STJ12138
 * @version 1.0
 * @description: TODO
 * @date 2023/7/11 10:54
 */
@RestController
@RequestMapping("/system/menu")
@Slf4j
@Api(tags = "菜单信息相关接口")
public class SysMenuController {
    @Autowired
    private ISysMenuService sysMenuService;

    /** 
     * @description: 获取菜单列表 
     * @param: sysMenu 
     * @return: com.huike.common.core.domain.AjaxResult<java.util.List<com.huike.common.core.domain.entity.SysMenu>> 
     * @author STJ12138
     * @date: 2023/7/11 12:06
     */
    @GetMapping("/list")
    @ApiOperation("获取菜单列表")
    public AjaxResult<List<SysMenu>> list(SysMenu sysMenu){
        //获取用户id
        Long userId = SecurityUtils.getUserId();
        List<SysMenu> list = sysMenuService.selectMenuListByUserId(sysMenu,userId);
        return AjaxResult.success(list);
    }

    /**
     * @description:  根据菜单编号获取详细信息
     * @param: menuId
     * @return: com.huike.common.core.domain.AjaxResult<com.huike.common.core.domain.entity.SysMenu>
     * @author STJ12138
     * @date: 2023/7/11 13:17
     */
    @GetMapping("/{menuId}")
    @ApiOperation("根据菜单编号获取详细信息")
    public AjaxResult<SysMenu> getByMenuId(@PathVariable Long menuId){
        log.info("菜单编号：{}",menuId);
        SysMenu sysMenu = sysMenuService.getByMenuId(menuId);
        return AjaxResult.success(sysMenu);
    }

    @GetMapping("/treeselect")
    @ApiOperation("获取菜单下拉树列表")
    public AjaxResult<TreeSelect> treeselect(SysMenu sysMenu){
        Long userId = SecurityUtils.getUserId();
        List<SysMenu> list = sysMenuService.selectMenuListByUserId(new SysMenu(), userId);
        List<TreeSelect> menus = sysMenuService.buildMenuTreeSelect(list);
        return AjaxResult.success(menus);
    }

    @GetMapping("/roleMenuTreeselect/{roleId}")
    @ApiOperation("加载对应角色菜单列表树")
    public RoleMenuTreeAjaxResult roleMenuTreeselect(@PathVariable Long roleId){
        //查询出最底层的菜单id
        List<Integer> checkedKeys = sysMenuService.roleMenuTreeselect(roleId);
        Long userId = SecurityUtils.getUserId();
        List<SysMenu> list = sysMenuService.selectMenuListByUserId(new SysMenu(), userId);
        List<TreeSelect> menus = sysMenuService.buildMenuTreeSelect(list);
        RoleMenuTreeAjaxResult roleMenuTreeAjaxResult = RoleMenuTreeAjaxResult.success();
        roleMenuTreeAjaxResult.setCheckedKeys(checkedKeys);
        roleMenuTreeAjaxResult.setMenus(menus);

        return roleMenuTreeAjaxResult;
    }

    /** 
     * @description: 新增菜单 
     * @param: sysMenu 
     * @return: com.huike.common.core.domain.AjaxResult 
     * @author STJ12138
     * @date: 2023/7/11 14:49
     */
    @PostMapping
    @ApiOperation("新增菜单")
    public AjaxResult addMenu(@RequestBody SysMenu sysMenu){
        sysMenuService.addMenu(sysMenu);
        return AjaxResult.success();
    }

    /**
     * @description: 修改菜单
     * @param: sysMenu
     * @return: com.huike.common.core.domain.AjaxResult
     * @author STJ12138
     * @date: 2023/7/11 15:02
     */
    @PutMapping
    @ApiOperation("修改菜单")
    public AjaxResult updateMenu(@RequestBody SysMenu sysMenu){
        sysMenuService.updateMenu(sysMenu);
        return AjaxResult.success();
    }

    /**
     * @description: 删除菜单.存在子菜单或已分配,不允许删除
     * @param: menuId
     * @return: com.huike.common.core.domain.AjaxResult
     * @author STJ12138
     * @date: 2023/7/11 15:09
     */
    @DeleteMapping("/{menuId}")
    @ApiOperation("删除菜单.存在子菜单或已分配,不允许删除")
    public AjaxResult deleteMenu(@PathVariable Long menuId){
        sysMenuService.deleteMenu(menuId);
        return AjaxResult.success();
    }
}
